package com.zryc.shop.oms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.oms.entity.OrderReturnReason;
import com.zryc.shop.oms.service.IOrderReturnReasonService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 * 退货原因表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/oms/returnReason")
@Api(tags = "退货原因管理")
public class OrderReturnReasonController {
    @Autowired
    private IOrderReturnReasonService returnReasonService;

    @GetMapping("/list")
    @ApiOperation("分页查询列表")
    public ResponseResult list(@RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示条数", required = true) Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        returnReasonService.page(page);
        return new ResponseResult(200, "查询完成", page);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询详情")
    public ResponseResult info(@PathVariable Long id) {
        return new ResponseResult(200, "查询完成", returnReasonService.getById(id));
    }

    @PostMapping("/create")
    @ApiOperation("添加退货原因")
    public ResponseResult create(@RequestBody OrderReturnReason returnReason) {
        returnReason.setCreateTime(LocalDateTime.now());
        returnReasonService.save(returnReason);
        return new ResponseResult(200, "添加成功");
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseResult delete(@RequestParam String ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids.split(","));
        returnReasonService.remove(queryWrapper);
        return new ResponseResult(200, "删除成功");
    }

    @PostMapping("/update/status")
    @ApiOperation("修改状态")
    public ResponseResult updateStatus(@RequestParam String ids, Integer status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.in("id", ids.split(","));
        updateWrapper.set("status", status);
        returnReasonService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改退货原因")
    public ResponseResult update(@PathVariable Long id, @RequestBody OrderReturnReason returnReason) {
//        returnReason.setId(id);
        returnReasonService.updateById(returnReason);
        return new ResponseResult(200, "修改成功");
    }

}
